package com.ycz.zheng_springboot.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Quarter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ycz.zheng_springboot.entity.common.Result;
import com.ycz.zheng_springboot.entity.User;
import com.ycz.zheng_springboot.service.IUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/echarts")
public class EchartsController {
    @Autowired
    private IUserService userService;

    //测试数据
    @GetMapping("/getEchartsData")
    public Result getEchartsData() {
        Map<String, Object> map = new HashMap<>();
        map.put("x", CollUtil.newArrayList("星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天)"));
        map.put("y", CollUtil.newArrayList(5, 20, 36, 10, 75, 90, 100));
        return Result.success(map);
    }

    /**
     * 查询每个季度用户数量
     * @return
     */
    @GetMapping("/members")
    public Result members() {
        List<User> list = userService.list();
        int q1 = 0;//第一季度
        int q2 = 0;//第二季度
        int q3 = 0;//第三季度
        int q4 = 0;//第四季度
        for (User user : list) {
            Date createTime = user.getCreateTime();
            Quarter quarter = DateUtil.quarterEnum(createTime);//获取季度
            if (quarter == Quarter.Q1) {
                q1++;
            } else if (quarter == Quarter.Q2) {
                q2++;
            } else if (quarter == Quarter.Q3) {
                q3++;
            } else if (quarter == Quarter.Q4) {
                q4++;
            } else {
                System.out.println("未知季度");
            }
        }
        return Result.success(CollUtil.newArrayList(q1, q2, q3, q4));
    }

    /**
     * 查询用户数量
     * @return
     */
    @GetMapping("/allMembers")
    public Result getCountPeople() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        int totalUser = (int) userService.count(queryWrapper);
       // System.out.println("totalUser = " + totalUser);
        return Result.success(totalUser);
    }


}
